/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.Toolkit;

/**
 *
 * @author Vincent
 */
public class DialogueSauvegarder extends javax.swing.JFrame {
    SimIHM ihm;
    /**
     * Creates new form DialogueSauvegarder
     */
    public DialogueSauvegarder(SimIHM ihm) {
        this.ihm = ihm;
        String[] noms;
        initComponents();
        
        //Centre la fenetre
        setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-getWidth()/2,
                Toolkit.getDefaultToolkit().getScreenSize().height/2-getHeight()/2);
        
        //Charge les noms des simulations existantes dans la combobox
        noms = ihm.menu.co.nomSimulations();
        for (String i : noms){
            if (i==null) {continue;}
            //System.out.println(i);
            comboBoxSimulations.addItem(i);
        }
        
        textFieldNomSauvegarde.setText(ihm.nomSimu);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelNomSauvegarde = new javax.swing.JLabel();
        textFieldNomSauvegarde = new javax.swing.JTextField();
        comboBoxSimulations = new javax.swing.JComboBox();
        boutonSauvegarder = new javax.swing.JButton();
        boutonAnnuler = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        labelNomSauvegarde.setText("Nom de la sauvegarde :");

        comboBoxSimulations.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxSimulationsActionPerformed(evt);
            }
        });

        boutonSauvegarder.setText("Sauvegarder");
        boutonSauvegarder.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                boutonSauvegarderMouseReleased(evt);
            }
        });

        boutonAnnuler.setText("Annuler");
        boutonAnnuler.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                boutonAnnulerMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(boutonSauvegarder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boutonAnnuler, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textFieldNomSauvegarde, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboBoxSimulations, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(labelNomSauvegarde))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelNomSauvegarde)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textFieldNomSauvegarde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxSimulations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boutonSauvegarder)
                    .addComponent(boutonAnnuler))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boutonSauvegarderMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_boutonSauvegarderMouseReleased
        // TODO add your handling code here:
        int id = ihm.menu.co.sauvegarderSimulation(textFieldNomSauvegarde.getText(), ihm.sim.getSimMode(), ihm.sim.getTailleGrille());
        String query = ihm.sim.requeteSauvegarde(id);
        ihm.nomSimu = textFieldNomSauvegarde.getText();
        ihm.menu.co.sauvegarderGrille(query);
        ihm.setVisible(true);
        dispose();
    }//GEN-LAST:event_boutonSauvegarderMouseReleased

    private void comboBoxSimulationsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxSimulationsActionPerformed
        // TODO add your handling code here:
        textFieldNomSauvegarde.setText(comboBoxSimulations.getSelectedItem().toString());
    }//GEN-LAST:event_comboBoxSimulationsActionPerformed

    private void boutonAnnulerMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_boutonAnnulerMouseReleased
        // TODO add your handling code here:
        ihm.setVisible(true);
        dispose();
    }//GEN-LAST:event_boutonAnnulerMouseReleased

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boutonAnnuler;
    private javax.swing.JButton boutonSauvegarder;
    private javax.swing.JComboBox comboBoxSimulations;
    private javax.swing.JLabel labelNomSauvegarde;
    private javax.swing.JTextField textFieldNomSauvegarde;
    // End of variables declaration//GEN-END:variables
}
